package cn.appsys.controller;

import cn.appsys.pojo.DevUser;
import cn.appsys.service.DevUser.DevUserService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Log4j
@Controller
@RequestMapping("/devuser")
public class DevUserController {
    @Autowired
    private DevUserService devUserService;

    @PostMapping("/login")
    public String login(HttpServletRequest request, HttpSession session, @RequestParam String devCode, @RequestParam String devPassword){
        log.debug("开发者登录");
        DevUser user = this.devUserService.getLoginUser(devCode, devPassword);
        if (user != null) {
            if (user.getDevPassword().equals(devPassword)) {
                session.setAttribute("devSession", user);
                return "kaifaindex";
            } else {
                request.setAttribute("message","密码错误");
                return "kaifalogin";
            }
        } else {
            request.setAttribute("message","账号不存在");
            return "kaifalogin";
        }
    }
    @RequestMapping("/toMain")
    public String main(HttpSession session) {
        if (session.getAttribute("devSession") == null) {
            return "redirect:/devuser/toLogin";
        } else {
            log.debug("跳转到开发者首页");
            return "kaifaindex";
        }
    }
    @RequestMapping("/toLogin")
    public String login() {
        log.debug("跳转到开发者登录页面");
        return "kaifalogin";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session, HttpServletRequest request) {
        log.debug("开发者注销");
        request.getSession().invalidate();
        return "index";
    }
}
